package com.px.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.px.dal.AwardsMapper;
import com.px.model.AwardInfo;
import com.px.model.getData;
import com.px.unit.Response;

@Controller
public class AwardsInfoController {
	AwardsMapper awards_Mapper = new AwardsMapper();

	/**
	 * 获取所有奖项信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/getAllAwards", method = RequestMethod.GET, produces = "application/json; utf-8")
	public Response getAllAwards() {
		// 示例化一个自定义对象，用来传递我们需要的参数
		getData mydata = new getData();
		List<AwardInfo> awardslist = awards_Mapper.getAllAwards();
		mydata.setAwardslist(awardslist);
		return new Response(200, "获取成功！", mydata);
	}

	/**
	 * 添加奖项
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/addAwardInfo", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response addAwardInfo(@RequestBody AwardInfo awards) {

		if (awards != null) {

			// 判断添加的奖项不能重复
			int checkname = awards_Mapper.checkAdwardsIdIsExist(awards);
			if (checkname > 0) {
				return new Response(304, "已存在同名奖项");
			} else {
				int counts = awards_Mapper.insertAwards(awards);
				if (counts > 0) {
					return new Response(200, "添加成功！");
				} else {
					return new Response(304, "添加失败！");
				}
			}
		}
		return new Response(304, "添加失败！");
	}

	/**
	 * 删除奖项信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/deleteAwardInfo", method = RequestMethod.DELETE, produces = "application/json; utf-8")
	public Response deleteAwardInfo(@RequestBody AwardInfo awards) {

		if (awards != null) {

			// 判断删除的奖项是否存在
			int checkname = awards_Mapper.checkAdwardsIdIsExist(awards);
			if (!(checkname > 0)) {
				return new Response(304, "该奖项不存在");
			} else {
				int counts = awards_Mapper.deleteAdwardsByAdardsId(awards.getAwards_id());
				if (counts > 0) {
					return new Response(200, "删除成功！");
				} else {
					return new Response(304, "删除失败！");
				}
			}
		}
		return new Response(304, "删除失败！");
	}

	/**
	 * 修改奖项信息
	 * 
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/updateAwardInfo", method = RequestMethod.PUT, produces = "application/json; utf-8")
	public Response updateAwardInfo(@RequestBody AwardInfo awards) {

		if (awards != null) {

			// 判断修改的奖项是否存在
			int checkid = awards_Mapper.checkAdwardsIdIsExist(awards);
			if (!(checkid > 0)) {
				return new Response(304, "该奖项不存在");
			} else {
				int counts = awards_Mapper.updateAdwardsByAdardsName(awards);
				if (counts > 0) {
					return new Response(200, "修改成功！");
				} else {
					return new Response(304, "修改失败！");
				}
			}
		}
		return new Response(304, "修改失败！");
	}

	/*
	 * 按照id查询奖项信息
	 */
	@ResponseBody
	@CrossOrigin
	@RequestMapping(value = "/selectAdwardsByAdardsId", method = RequestMethod.POST, produces = "application/json; utf-8")
	public Response selectAdwardsByAdardsId(@RequestBody AwardInfo awards) {
		if (awards != null) {
			// 判断查询的参选会员单位是否存在
			int checkid = awards_Mapper.checkAdwardsIdIsExist(awards);
			if (!(checkid > 0)) {
				return new Response(304, "该奖项不存在");
			} else {
				// 示例化一个自定义对象，用来传递我们需要的参数
				getData mydata = new getData();
				List<AwardInfo> awardslist = awards_Mapper.selectAdwardsByAdardsId(awards);
				mydata.setAwardslist(awardslist);
				return new Response(200, "获取成功！", mydata);
			}
		}
		return new Response(304, "获取失败！");

	}
}
